<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label-width="auto" label="订单流水号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入订单流水号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品ID " prop="spuId">
        <el-input
          v-model="queryParams.spuId"
          placeholder="请输入商品ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称 " prop="spuName">
        <el-input
          v-model="queryParams.spuName"
          placeholder="请输入商品名称 "
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="用户名" align="center" prop="userName"/>
      <el-table-column label="订单流水号" align="center" prop="no"/>
      <el-table-column label="商品ID" align="center" prop="spuId"/>
      <el-table-column label="商品名称 " align="center" prop="spuName"/>
      <el-table-column label="数量" align="center" prop="count"/>
      <el-table-column label="商品图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="第三方支付流水号(如支付宝)" align="center" prop="payTradeNo"/>
      <el-table-column label="订单支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付成功的支付渠道" align="center" prop="payChannel"/>

      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <p>{{status_dict[scope.row.status - 1]['label']}}</p>
        </template>
      </el-table-column>

      <el-table-column label="订单完成时间" align="center" prop="finishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总价" align="center" prop="totalPoint"/>
      <el-table-column label="实际付款积分" align="center" prop="actualPoint"/>
      <el-table-column label="发货时间" align="center" prop="deliveryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货时间" align="center" prop="receiveTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.receiveTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="快递方式" align="center" prop="logisticsCode"/>
      <el-table-column label="快递单号" align="center" prop="logisticsNo"/>
      <el-table-column label="收件人名称" align="center" prop="receiverName"/>
      <el-table-column label="收件人手机" align="center" prop="receiverMobile"/>
      <el-table-column label="收件人详细地址" align="center" prop="receiverDetailAddress"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.status === '2'">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['qiushi:mall:tradepointorder:edit']"
          >发货
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改积分商品订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="快递方式" prop="logisticsCode">
          <el-input v-model="form.logisticsCode" placeholder="请输入快递方式"/>
        </el-form-item>
        <el-form-item label="快递单号" prop="logisticsNo">
          <el-input v-model="form.logisticsNo" placeholder="请输入快递单号"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOrder, getPointOrder, delOrder, addOrder, updateOrder} from "@/api/qiushi/mall/tradepointorder";

export default {
  name: "Tradepointorder",
  data() {
    return {
      // 字典
      status_dict: [{
        value: '1',
        label: '待付款'
      },{
        value: '2',
        label: '待发货'
      },{
        value: '3',
        label: '待收货'
      },{
        value: '4',
        label: '已取消'
      },{
        value: '5',
        label: '已完成'
      },],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 积分商品订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        no: null,
        spuId: null,
        spuName: null,
        count: null,
        image: null,
        payTradeNo: null,
        payTime: null,
        payChannel: null,
        status: null,
        finishTime: null,
        totalPoint: null,
        actualPoint: null,
        deliveryTime: null,
        receiveTime: null,
        logisticsCode: null,
        logisticsNo: null,
        receiverName: null,
        receiverMobile: null,
        receiverDetailAddress: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          {required: true, message: "用户编号不能为空", trigger: "blur"}
        ],
        no: [
          {required: true, message: "订单流水号不能为空", trigger: "blur"}
        ],
        spuId: [
          {required: true, message: "商品id 不能为空", trigger: "blur"}
        ],
        spuName: [
          {required: true, message: "商品名称 不能为空", trigger: "blur"}
        ],
        count: [
          {required: true, message: "数量不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "1待付款 2待发货 3待收货 4已取消 5已完成 不能为空", trigger: "change"}
        ],
        totalPoint: [
          {required: true, message: "总价不能为空", trigger: "blur"}
        ],
        receiverName: [
          {required: true, message: "收件人名称不能为空", trigger: "blur"}
        ],
        receiverMobile: [
          {required: true, message: "收件人手机不能为空", trigger: "blur"}
        ],
        delFlag: [
          {required: true, message: "0正常 1删除不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询积分商品订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        no: null,
        spuId: null,
        spuName: null,
        count: null,
        image: null,
        payTradeNo: null,
        payTime: null,
        payChannel: null,
        status: null,
        finishTime: null,
        totalPoint: null,
        actualPoint: null,
        deliveryTime: null,
        receiveTime: null,
        logisticsCode: null,
        logisticsNo: null,
        receiverName: null,
        receiverMobile: null,
        receiverDetailAddress: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,

        userName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加积分商品订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPointOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改积分商品订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除积分商品订单编号为"' + ids + '"的数据项？').then(function () {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('qiushi/mall/tradepointorder/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
